<?php
require_once("../config.php");
require_once("../facebook.php");
session_start();
if(empty($_SESSION['facebook_id']))
	die("Not logged into facebook.");
$facebook_id = $_SESSION['facebook_id'];
?>



<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
	<div id="profile_pics"></div>
	<head>
		<script type="text/javascript" src=<?php echo LIB_JQUERY; ?>></script>
		<script type="text/javascript">


function getFriends() {
				var query = FB.Data.query('SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = {0}) AND is_app_user = 1',
				                           <? echo $facebook_id; ?>);
				query.wait(function(rows) { // query.wait will pass an indexed array of the results of the query
					$('.friend').remove();
					for(var i = 0; i < rows.length; i++) {
						$('.friend_list').append('<li class="friend">'+ "<a href = 'javascript:sendNotification(" + rows[i].uid + ")'>" + rows[i].name + '' + '<img src="' +  rows[i].pic_square + '" + id =>' + '</li>'); // Removes the friend class elements and adds the li elements to the friend_list class 
					} // done to avoid duplicates. 	This will also return the friend list with a clickable link for each name to send that friend a notification			
				});
		}
// Function that takes in the friend  facebook ID of who to send a request (notification) to and sends the request
function sendNotification(friendFbID) {
	FB.ui({method: 'apprequests', to: friendFbID, message: 'Looking to trade', data: 'tracking information for the user'});
}

// This function will pull up a request dialog filtered by friends that do not have the app installed
// Users can then select the friends they want to invite to use the app
function inviteToApp() {
FB.ui({ method: 'apprequests', filters: ['app_non_users'], 
       message: 'You should try this app out'});
 }


</script>
</head>

<body>
		<div id="fb-root"></div>
		<script>
			// asynchronously loads Facebook Javascript SDK, then displays card/friends list.
			window.fbAsyncInit = function() {
				FB.init({
					appId  : <?=APP_ID;?>, // currently our FB app ID
					status : true, // check login status
					cookie : true, // enable cookies to allow the server to access the session
					xfbml  : true  // parse XFBML
				});
			};
			
			(function() {
				var e = document.createElement('script');
				e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
				e.async = true;
				document.getElementById('fb-root').appendChild(e);
			}());
		</script>
		

		<input id="test" type="submit" value="Test Show Friends" onclick="getFriends()"/> 
		<input id="request" type="submit" value="Invite friends to use the Untold app" onclick = "inviteToApp()"/>
		<a href="testList.php">Back to list</a><br><br>
		<ul class="friend_list"></ul>
	</body>
</html>

